package com.bridge.file;

import com.bridge.NetDiskUrl;
import com.bridge.jdbc.JDBCUtils;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtils {

    private static String title = "";
    private static String subTitle = "";

    public static void readText() throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\temp.txt");
        FileInputStream fis = new FileInputStream(file);
        //Construct BufferedReader from InputStreamReader
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String line = null;
        List<NetDiskUrl> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            NetDiskUrl netDiskUrl = extractUrls(line);
            if (netDiskUrl != null) {
                list.add(netDiskUrl);
//                System.out.println(netDiskUrl.toString());
            }
        }
        br.close();
        JDBCUtils.batchCommitInsert(list);
    }

    public static NetDiskUrl extractUrls(String input) {
        if (!StringUtils.hasText(input)) {
            return null;
        }
        input = input.trim();
        if (input.startsWith("####") && !input.startsWith("#####")) {
            if (input.contains("、")) {
                String[] split = input.split("、");
                title = split[1];
                subTitle = "";
            }
        }
        if (input.startsWith("#####")) {
            if (input.contains("、")) {
                String[] split = input.split("、");
                subTitle = split[1];
            }
        }

        String code = "";
        if (input.contains("提取码")) {
            code = input.substring(input.length() - 4);
        }

        Pattern pattern = Pattern.compile(
                "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
                        "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
                        "|mil|biz|info|mobi|name|aero|jobs|museum" +
                        "|travel|[a-z]{2}))(:[\\d]{1,5})?" +
                        "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
                        "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
                        "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
                        "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
                        "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
                        "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            NetDiskUrl netDiskUrl = new NetDiskUrl();
            netDiskUrl.setTitle(title);
            netDiskUrl.setSubTitle(subTitle);
            netDiskUrl.setUrl(matcher.group());
            netDiskUrl.setCode(code);
            return netDiskUrl;
        }
        return null;
    }

    public static void main(String[] args) {
        try {
            // 都文本
            FileUtils.readText();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
